var page_controller;
var alert_controller = new Array();
var UA = navigator.userAgent.toLowerCase();//UserAgent
var heroOptionArray = [[1,"王昭君",3],[2,"小乔",3],[3,"武则天",3],[4,"韩信",6],[5,"程咬金",6],[6,"赵云",8],[7,"老夫子",8],[8,"项羽",40],[9,"鲁班",80]];
var numOptionArray = [[10,"单",2],[11,"双",2],[12,"大",2],[13,"小",2],[14,"顺子",30],[15,"三条",30]];
var $_user = new Object();

document.addEventListener('touchmove',function(e){
    e.preventDefault();//移除浏览器默认操作
},false);
function initPage(){   
    var objLoading = document.getElementById("loadingbox");   
    if (objLoading != null)   {   
        //setTimeout(function(){
            //$('.loadingbox').animate({'top':'2%','opacity':'0'},50);
            //setTimeout(function(){
                $('.loadingbox').hide();    
            //},50);
        //},0);
    }
}
window.onload = function(){
    FastClick.attach(document.body);//加载FastClick插件
    bindClickEvent();//绑定共用部分点击事件
    page_onload();//加载页面onload事件
}
function iScrollClick(){
    //iScroll click 兼容性问题
    if(/iPhone|iPad|iPod|Macintoch/i.test(UA)) return false;
    if(/Chrome/i.test(UA)) return true;//(/Android/i.test(UA))
    if(/Silk/i.test(UA)) return false;
    if(/Android/i.test(UA)){
        var s = UA.match(/Android [\d+.]{1,5}/)[0].replace('Android','');
        return parseFloat(s[0]+s[2]+s[4])<=442&&parseFloat(s[0]+s[2]+s[4])>430 ? true:false;
    }
}
function bindClickEvent(){
    getUserInfo();
    $('.alert_close').on('click',function(){
        $(this).parent().parent().parent().fadeOut(200);
    });
    $('.chest_open_view>.close_btn').on('click',function(){
        $(this).parent().fadeOut(200);
    });
    if($('.alert_body').length){
        if($('.alert_content').length){
            for(var i=0;i<$('.alert_content').length;i++){
                $('.alert_content:eq('+i+')').attr('id','iScroll');
                alert_controller[i] = new IScroll('#iScroll',{
                    mouseWheel:true,
                    scrollbars:false,
                    bounce:false,
                    click:iScrollClick()
                });
                $('#iScroll').attr('id','');
           }
        }
    }
}
function refreshAlertIScroll(){
    if($('.alert_content').length){
        for(var i=0;i<$('.alert_content').length;i++){
            alert_controller[i].refresh();
       }
    }
}
function errorMsg(msg){
    var randomId = parseInt(Math.random()*1000000+1);
    $('body').append("<div class='error_box' id='"+randomId+"'><span>"+msg+"</span></div>");
    $('#'+randomId).fadeIn(200);
    setTimeout(function(){
        $('#'+randomId).fadeOut(200);
    },1200); 
}
function loadImage(url){//预加载图片
    var img = new Image(); 
    img.src = url; 
} 
var $_GET = (function(){
    var url = window.document.location.href.toString();
    var u = url.split("?");
    if(typeof(u[1]) == "string"){
        u = u[1].split("&");
        var get = {};
        for(var i in u){
            var j = u[i].split("=");
            get[j[0]] = j[1];
        }
        return get;
    } else {
        return {};
    }
})();

function setCookie(name,value) { 
    var Days = 1; 
    var exp = new Date(); 
    exp.setTime(exp.getTime() + Days*24*60*60*1000); 
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); 
} 

//读取cookies 
function getCookie(name) { 
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]); 
    else 
        return null; 
} 

//删除cookies 
function delCookie(name) { 
    var exp = new Date(); 
    exp.setTime(exp.getTime() - 1); 
    var cval=getCookie(name); 
    if(cval!=null) 
        document.cookie= name + "="+cval+";expires="+exp.toGMTString(); 
} 
function String2Array(stringObject){
    stringObject = stringObject.replace(/\"/g, "");
    stringObject = stringObject.substr(2,stringObject.length-4);
    var stringArray = stringObject.split("],[");
    var returnArray = new Array();
    for(var i = 0 ; i < stringArray.length; i++){
        var thisArray = stringArray[i].split(",");
        returnArray.push(thisArray);
    }
    return returnArray;
}
function getUserInfo(){
    $.ajax({
        url:"/login/getUserInfo",
        type:'GET',
        dataType : 'json',
        success: function(data){
            $_user = data;
            //alert($_user);
        },
        error: function(e){
            errorMsg('获取用户数据失败');
        }
    });
}
function checkLogin(sucfunc = '',errfunc = ''){
    $.ajax({
        url:"/trade/iflogin",
        type:'GET',
        dataType : 'json',
        success: function(data){
            if(data.status == 1){
                if(sucfunc){
                    sucfunc();   
                } 
            }else{
                if(errfunc){
                    errfunc();   
                }
            }
        },
        error: function(e){
            if(errfunc){
                errfunc();   
            }
        }
    });
}